package com.alipay.sofa.award.listener;

import com.alipay.sofa.award.biz.service.facade.ActivityAwardFacade;
import com.alipay.sofa.award.biz.service.facade.ActivityTimeFacade;
import com.alipay.sofa.award.common.dal.dataobject.ActivityTime;
import com.alipay.sofa.award.common.dal.dataobject.Award;
import com.alipay.sofa.award.biz.service.enums.ActivityTimeEnums;
import com.alipay.sofa.award.biz.service.enums.AwardEnums;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.List;

/**
 * AwardContextRefreshedEventListener
 *
 * @author yangguanchao
 * @since 2018/09/03
 */
public class AwardContextRefreshedEventListener implements
                                               ApplicationListener<ContextRefreshedEvent> {

    private static final Logger logger = LoggerFactory
                                           .getLogger(AwardContextRefreshedEventListener.class);

    @Autowired
    private ActivityTimeFacade  activityTimeFacade;

    @Autowired
    private ActivityAwardFacade activityAwardFacade;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //already activity initialized
        List<ActivityTime> activityTimeList = this.activityTimeFacade.queryAllActivityTime();
        if (activityTimeList != null && activityTimeList.size() > 0) {
            logger
                .warn("Activity time table and award table has bean initialized! So ignore init by sql in java code and activity time list size = "
                      + "[" + activityTimeList.size() + "]");
            return;
        }
        //activity time
        for (ActivityTimeEnums activityTimeEnums : ActivityTimeEnums.values()) {
            //insert or not
            ActivityTime activityTime = new ActivityTime();
            String activityTimeStr = activityTimeEnums.getActivityTime();
            activityTime.setActivityTime(activityTimeStr);
            activityTime.setActivityDesc(activityTimeEnums.getActivityDesc());
            this.activityTimeFacade.insertActivityTimeWhenNoExist(activityTime);
            //award
            for (AwardEnums awardEnums : AwardEnums.values()) {
                //insert
                Award award = new Award();
                //activity time
                award.setActivityTime(activityTimeStr);
                String rank = awardEnums.getRank();
                award.setRank(rank);
                award.setTotalCount(awardEnums.getTotalCount());
                award.setAwardCount(awardEnums.getAwardCount());
                award.setAwardContent(awardEnums.getAwardContent());
                award.setAwardDesc(awardEnums.getAwardDesc());
                //insert or update with the activityTime and rank
                this.activityAwardFacade.insertWhenNoExistWithActivityTimeAndRank(award);
            }
        }

    }
}
